﻿using Extentions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TrainingReminderSystem.Model;

namespace TrainingReminderSystem.Bll
{
    public class AllEnumBll
    {
        private const string cacheKey = "AllEnum";

        /// <summary>
        /// 得到所有的Enum对象集合 比如var EnumOpenIDType=[{Value:0,Label:"微信公众号"},{Value:1,Label:"小程序"}];
        /// </summary>
        /// <returns></returns>
        public string GetAllEnum()
        {
            var r = CacheHelper.GetCache(cacheKey) as string;
            if (!string.IsNullOrEmpty(r))
            {
                return r;
            }

            Assembly ass = typeof(EnumStatus).Assembly;
            var types = ass.GetTypes();
            foreach (var type in types)
            {
                if (type.IsEnum == true)
                {
                    var jsString = EnumExtentions.GetAllKeyValue(type, false).ExToJson();
                    var fullJsString = $"var {type.Name}={jsString};\r\n";
                    r += fullJsString;
                }
            }
            r = r.ExTrim();
            CacheHelper.SetRelativeCache(cacheKey, r);
            return r;
        }
    }
}
